package com.googlecode.recycled.lang.jse.api.io;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

import java.io.File;
import java.io.FileFilter;

public class ChainedFileFilter implements FileFilter {

    protected Iterable<FileFilter> filters;
    protected Matcher matcher;

    public ChainedFileFilter(Matcher matcher, Iterable<FileFilter> filters) {
        this.matcher = matcher;
        this.filters = filters;
    }

    public ChainedFileFilter(Matcher matcher, FileFilter... filters) {
        this(matcher, Lists.newArrayList(filters));
    }

    @Override
    public boolean accept(File file) {
        if (matcher == Matcher.ALL) {
            return Iterables.all(filters, new FileFilterPredicate(file));
        }
        return Iterables.any(filters, new FileFilterPredicate(file));
    }

    public static enum Matcher {
        ALL, ANY;
    }

}